﻿define([
    'jquery',
    'kendo',
    'moment',
    'certificate',
    'quali',
    'ajax-pre'],
    function ($, kendo, moment, certificate, quali) {
        var certificateType = JSON.parse(document.getElementById('certificateTypes').value);
        var treeDataSource = JSON.parse(document.getElementById('treeDataSource').value);
        var allDepartments = JSON.parse(document.getElementById('allDepartments').value);

        var sex = JSON.parse(document.getElementById('sex').value);
        var posts = JSON.parse(document.getElementById('posts').value);
        var dataSource = new kendo.data.DataSource({
            schema: {
                data: function (response) {
                    return response.Datas || response.Id || response.Data || response;
                },
                model: {
                    id: "Id",
                    fields: {
                        Id: { nullable: false },
                        Name: { validation: { required: true } },
                        Number: { validation: { required: true } },
                        DepartmentId: { validation: { required: false } },
                        Sex: { validation: { required: true } },
                        PositionId: { validation: { required: false }, defaultValue: "" },
                        Phone: { validation: { required: true }, defaultValue: null },
                        BirthDay: { type: "date", validation: { required: true }, defaultValue: new Date() },
                    }
                }
            },
            pageSize: 20,
            serverPaging: true,
            batch: true,
            transport: {
                read: {
                    url: "/User",
                    dataType: "json",
                    method: "post"
                },
                update: {
                    url: "/User/Save",
                    dataType: "json",
                    method: "post",
                    contentType: "application/json"
                },
                destroy: {
                    url: "/User/Remove",
                    dataType: "json",
                    method: "post",
                    contentType: "application/json"
                },
                create: {
                    url: "/User/Save",
                    dataType: "json",
                    method: "post",
                    contentType: "application/json"
                },
                parameterMap: function (options, operation) {
                    if (operation !== "read" && options.models) {
                        return JSON.stringify({ users: options.models });
                    }
                    if (operation == "read") {
                        return {
                            page: {
                                page: options.page, pages: options.pageSize
                            }
                        };
                    }
                }
            }
        });

        $("#grid").kendoGrid({
            dataSource: dataSource,
            filterable: true,
            toolbar: [{ name: "create", text: '新建' }, { name: "save", text: '保存' }],
            pageable: {
                refresh: true,
                pageSizes: true,
                buttonCount: 5
            },
            columns: [{ field: "Name", title: "姓名", width: '110px' },
            { field: "Number", title: "工号", width: '160px' },
            {
                field: "Sex", title: "性别", width: '100px',
                editor: function (container, options) {

                    $('<input data-bind="value:' + options.field + '" />').appendTo(container)
                    .kendoComboBox({
                        //placeholder: "顶级部门",
                        dataTextField: "Name",
                        dataValueField: "Value",
                        dataSource: sex
                    });
                }
            },
             {
                 field: "DepartmentId", title: "部门", width: '220px',
                 template: function (value) {
                     var results = "-";
                     $.each(allDepartments, function (index, departmentDto) {
                         if (departmentDto.Id && departmentDto.Id == value.DepartmentId) {
                             results = departmentDto.Name;
                             return;
                         }
                     });
                     return results;
                 },
                 editor: function (container, options) {
                     $('<input data-bind="value:' + options.field + '" />').appendTo(container)
                     .kendoComboBox({
                         dataTextField: "Name",
                         dataValueField: "Id",
                         dataSource: allDepartments
                     });
                 }
             },
             {
                 field: "PositionId", title: "岗位", width: '150px',
                 editor: function (container, options) {

                     $('<input data-bind="value:' + options.field + '" />').appendTo(container)
                     .kendoComboBox({
                         dataTextField: "PostName",
                         dataValueField: "Id",
                         dataSource: posts
                     });
                 },
                 template: function (value) {
                     if (posts && value.PositionId) {
                         for (var i = 0; i < posts.length; i++) {
                             var itemPost = posts[i];
                             if (itemPost.Id == value.PositionId) {
                                 return itemPost.PostName;
                             }
                         }
                     } else {
                         return "";
                     }
                 }
             },
              { field: "Phone", title: "电话", width: '150px' },
              {
                  field: "BirthDay", title: "生日", width: '150px',
                  template: function (value) {
                      return new moment(value.BirthDay).format('YYYY-MM-DD');
                  },
                  editor: function (container, options) {
                      $('<input data-bind="value:' + options.field + '" />').appendTo(container)
                      .kendoDatePicker({
                          format: 'yyyy-MM-dd',
                          interval: 1
                      });
                  }
              },
            {
                command: [
                  { name: "destroy", 'text': '删除' },
                  { name: "edit", text: '编辑' },
                  { name: "custom", text: '证件管理', click: showCertification },
                  { name: "quali", text: '资质/技能', click: showQuali },
                ]
            }
            ],
            editable: { //disables the deletion functionality
                update: true,
                destroy: true,
                create: true,
                mode: 'inline',// inline popup
            },
            //editable: "popup",
            pageable: false,

        });

        function showCertification(e) {
            e.preventDefault();
            var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
            var myWindow = $("#window");
            function onClose() {
                $(this).fadeIn();
            }

            myWindow.kendoWindow({
                width: "1050px",
                title: "证件管理",
                visible: false,

                actions: [
                    "Minimize",
                    "Maximize",
                    "Close"
                ],
                close: onClose,
                open: function (e) {
                    this.wrapper.css({ top: 100 });
                },
                modal: true
            }).data("kendoWindow").center().open();

            certificate(dataItem.Id, '#windoGrid', certificateType);
        }
        function showQuali(e) {
            e.preventDefault();
            var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
            var myWindow = $("#windowQuali");

            function onClose() {
                $(this).fadeIn();
            }

            myWindow.kendoWindow({
                width: "1100px",
                title: "资质管理",
                visible: false,
                actions: [
                    "Minimize",
                    "Maximize",
                    "Close"
                ],
                close: onClose,
                open: function (e) {
                    this.wrapper.css({ top: 100 });
                },
                modal: true
            }).data("kendoWindow").center().open();

            quali(dataItem.Id, '#windoGrid2');
        }



    });